package PrjTest

import (
	"bytes"
	"dgo/goutils/utils"
	"io"
	"net/http"
	"net/url"
)

func NewHttpWAndR(rawurl string) (w http.ResponseWriter, r *http.Request) {
	r = new(http.Request)
	r.URL, _ = url.ParseRequestURI(rawurl)
	w = NewTestResponseWriter()
	return
}

func NewHttpWAndRWithBodyStr(rawurl, bodystr string) (w http.ResponseWriter, r *http.Request) {
	r = new(http.Request)
	r.URL, _ = url.ParseRequestURI(rawurl)
	reader := bytes.NewReader([]byte(bodystr))
	r.Body = NewReadCloser(reader)
	r.Header = make(map[string][]string)
	r.Header.Add("Content-Type", "application/json")
	w = NewTestResponseWriter()
	return
}

type TestResponseWriter struct {
	header http.Header
}

type TestReadCloser struct {
	reader io.Reader
}

func NewReadCloser(reader io.Reader) *TestReadCloser {
	rval := &TestReadCloser{reader: reader}
	return rval
}

func (this *TestReadCloser) Read(buf []byte) (int, error) {
	return this.reader.Read(buf)
}

func (this *TestReadCloser) Close() error {
	return nil
}

func NewTestResponseWriter() *TestResponseWriter {
	rval := &TestResponseWriter{header: make(http.Header)}
	return rval
}

func (this *TestResponseWriter) Header() http.Header {
	return this.header
}

func (this *TestResponseWriter) Write(buf []byte) (int, error) {
	utils.Log.Infof(string(buf))
	return len(buf), nil
}

func (this *TestResponseWriter) WriteHeader(statusCode int) {
	utils.Log.Infof("statusCode:%d", statusCode)
}
